package com.ctsi.ssdc.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctsi.ssdc.service.LitemallNoticeService;
import com.ctsi.ssdc.domain.LitemallNotice;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing LitemallNotice.
 *
 * @author ctsi-biyi-generator
 *
 */
@Api(value = "/api",tags = {"litemall-notice-controller"})
@RestController
@RequestMapping("/api")
public class LitemallNoticeController {

    private final Logger log = LoggerFactory.getLogger(LitemallNoticeController.class);

    private static final String ENTITY_NAME = "litemallNotice";

    private final LitemallNoticeService litemallNoticeService;

    public LitemallNoticeController(LitemallNoticeService litemallNoticeService) {
        this.litemallNoticeService = litemallNoticeService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /litemallNotices : Create a new litemallNotice.
     *
     * @param litemallNotice the litemallNotice to create
     * @return the ResponseEntity with status 201 (Created) and with body the new litemallNotice,
     * or with status 400 (Bad Request) if the litemallNotice has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallNotice",name = "litemallNotice",value = "the litemallNotice to create")
    })
    @ApiOperation(value = "POST  /litemallNotices : create a new litemallNotice.",
        notes = "POST  /litemallNotices : create a new litemallNotice.",httpMethod = "POST")
    @PostMapping("/litemallNotices")
    public ResponseEntity<LitemallNotice> createLitemallNotice(@RequestBody LitemallNotice litemallNotice) throws URISyntaxException {
        log.debug("REST request to save LitemallNotice : {}", litemallNotice);
        if (litemallNotice.getId() != null) {
            throw new BadRequestAlertException("A new litemallNotice cannot already have an id", ENTITY_NAME, "idexists");
        }
        LitemallNotice result = litemallNoticeService.insert(litemallNotice);
        return ResponseEntity.created(new URI("/api/litemallNotices" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /litemallNotices : Updates an existing litemallNotice.
     *
     * @param litemallNotice the litemallNotice to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated litemallNotice,
     * or with status 400 (Bad Request) if the litemallNotice is not valid,
     * or with status 500 (Internal Server Error) if the litemallNotice couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallNotice",
        name = "litemallNotice",value = "the litemallNotice to update")
    })
    @ApiOperation(value = "PUT  /litemallNotices : updates an existing litemallNotice.",
        notes = "PUT  /litemallNotices : updates an existing litemallNotice.",httpMethod = "PUT")
    @PutMapping("/litemallNotices")
    public ResponseEntity<LitemallNotice> updateLitemallNotice(@RequestBody LitemallNotice litemallNotice) throws URISyntaxException {
        log.debug("REST request to update LitemallNotice : {}", litemallNotice);
	    if (litemallNotice.getId() == null) {
	    	return createLitemallNotice(litemallNotice);
	    }
        LitemallNotice result = litemallNoticeService.update(litemallNotice);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * GET  /litemallNotices/:id : get the "id" litemallNotice.
     *
     * @param id the id of the litemallNotice to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the litemallNotice, or with status 404 (Not Found)
     */
//    @ApiImplicitParams({
//        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",name = "id",
//        value = "the id of the litemallNotice to retrieve")
//    })
//    @ApiOperation(value = "GET  /litemallNotices/id : get the id litemallNotice.",
//        notes = "GET  /litemallNotices/id : get the id litemallNotice.",httpMethod = "GET")
//    @GetMapping("/litemallNotices/{id}")
//    public ResponseEntity<LitemallNotice> getLitemallNotice(@PathVariable Integer id) {
//        log.debug("REST request to get LitemallNotice : {}", id);
//        LitemallNotice litemallNotice = litemallNoticeService.findOne(id);
//        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(litemallNotice));
//    }

    /**
    * GET  /litemallNotices : get the litemallNotices firstStringBaseColumn.
    *
    * @return the ResponseEntity with status 200 (OK) and the list of litemallNotices in body
    */
    @ApiOperation(value = "GET  /litemallNotices : get the litemallNotices firstStringBaseColumn.",
                notes = "GET  /litemallNotices : get the litemallNotices firstStringBaseColumn.",httpMethod = "GET")
    @GetMapping("/litemallNotices")
    public PageResult<LitemallNotice> getLitemallNoticesList(String title ,Pageable pageable) {
        log.debug("REST request to get LitemallNoticesList");
        return litemallNoticeService.findFirstStringColumn(title ,pageable);
    }
	
    /**
     * DELETE  /litemallNotices/:id : delete the "id" litemallNotice.
     *
     * @param id the id of the litemallNotice to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",
            name = "id",value = "the id of the litemallNotice to delete")
    })
    @ApiOperation(value = "DELETE  /litemallNotices/id : delete the id litemallNotice.",
        notes = "DELETE  /litemallNotices/id : delete the id litemallNotice.",httpMethod = "DELETE")
    @DeleteMapping("/litemallNotices/{id}")
    public ResponseEntity<Void> deleteLitemallNotice(@PathVariable Integer id) {
        log.debug("REST request to delete LitemallNotice : {}", id);
        litemallNoticeService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
